package net.wanho.controller;


import com.alibaba.fastjson.JSONObject;
import net.wanho.po.Order;
import net.wanho.service.OrderService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.wanho.vo.AjaxResult;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import lombok.RequiredArgsConstructor;



import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author 汤小洋
 * @since 2025-09-25
 */
@Api(tags = "08.订单表接口")
@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @ApiOperation("订单表分页查询")
    @GetMapping
    public AjaxResult findPage(@ApiParam("页码") @RequestParam(defaultValue = "1") int pageNum,
                              @ApiParam("页大小") @RequestParam(defaultValue = "5") int pageSize,
                              @ApiParam("查询条件") Order order){
        PageInfo<Order> pageInfo = orderService.findPage(pageNum,pageSize,order);

        Map<String,Object> map  =new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("pageNum", pageInfo.getPageNum());
        map.put("orders", pageInfo.getList());

        return AjaxResult.success(map);
    }

    @ApiOperation("根据主键查询订单表")
    @GetMapping("/{orderId}")
    public AjaxResult findById(@PathVariable Serializable orderId){
        return AjaxResult.success(orderService.findById(orderId));
    }

    @ApiOperation("新增订单表")
    @PostMapping
    public AjaxResult add(@RequestBody Order order){
        orderService.add(order);
        return AjaxResult.success();
    }

    @ApiOperation("修改订单表")
    @PutMapping
    public AjaxResult modify(@RequestBody Order order){
        orderService.modify(order);
        return AjaxResult.success();
    }

    @ApiOperation("根据主键删除订单表")
    @DeleteMapping("/{orderId}")
    public AjaxResult removeById(@PathVariable Serializable orderId){
        orderService.removeById(orderId);
        return AjaxResult.success();
    }

    @GetMapping("/logistics/{no}")
    public AjaxResult findLogistics(@PathVariable String no){
        JSONObject jsonObject = orderService.findLogistics(no);
        return AjaxResult.success(jsonObject);
    }

    @GetMapping("/reminder/{orderId}")
    public AjaxResult reminder(@PathVariable Integer orderId){
        orderService.reminder(orderId);
        return AjaxResult.success();
    }
}
